package com.edu.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@ApiModel("统一响应对象")
public class APIResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    @ApiModelProperty(
        value = "响应状态",
        example = "可选值参照枚举类：com.edu.myenum.StateEnum"
    )
    private int state;
    @ApiModelProperty(
        value = "请求地址",
        example = "/login"
    )
    private String request;
    @ApiModelProperty(
        value = "响应信息",
        example = "操作成功"
    )
    private String message;
    @ApiModelProperty(
        value = "异常信息",
        example = "操作失败，username为空"
    )
    private String cause;
    @ApiModelProperty(
        value = "返回变量-可返回非主体数据",
        example = "如返回数据ID"
    )
    private Map<String, Object> variables;
    @ApiModelProperty(
        value = "返回数据-主体数据",
        example = "如返回机构对象"
    )
    private T data;

    public APIResult() {
        this.state = StateEnum.SUCCESS.getCode();
        this.message = "";
        this.cause = "";
        this.variables = new HashMap();
    }

    public APIResult(int state) {
        this.state = StateEnum.SUCCESS.getCode();
        this.message = "";
        this.cause = "";
        this.variables = new HashMap();
        this.state = state;
    }

    public APIResult(int state, String message) {
        this.state = StateEnum.SUCCESS.getCode();
        this.message = "";
        this.cause = "";
        this.variables = new HashMap();
        this.state = state;
        this.message = message;
    }

    public APIResult(int state, String message, String cause) {
        this.state = StateEnum.SUCCESS.getCode();
        this.message = "";
        this.cause = "";
        this.variables = new HashMap();
        this.state = state;
        this.message = message;
        this.cause = cause;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return StateEnum.SUCCESS.getCode() == this.state;
    }

    @JsonIgnore
    public boolean isFailed() {
        return StateEnum.SUCCESS.getCode() != this.state;
    }

    public void addVariable(String key, Object value) {
        this.variables.put(key, value);
    }

    public Object getVariable(String key) {
        return null == this.variables ? null : this.variables.get(key);
    }

    public int getState() {
        return this.state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getRequest() {
        return this.request;
    }

    public void setRequest(String request) {
        this.request = request;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getCause() {
        return this.cause;
    }

    public void setCause(String cause) {
        this.cause = cause;
    }

    public Map<String, Object> getVariables() {
        return this.variables;
    }

    public void setVariables(Map<String, Object> vars) {
        this.variables = vars;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toJsonString() {
        return JacksonUtil.toJsonString(this);
    }

    public String toString() {
        return this.toJsonString();
    }
}
